import { reactive } from 'vue'

export const store = reactive({
  title: '',
  api: import.meta.env.VITE_API,//服务器接口地址
  www: import.meta.env.VITE_WWW,//服务器站点
  rootPath: import.meta.env.BASE_URL,
  homehash: '/frame/home',//初始首页
  pageRouter: null,
  pageOpen: '',
  pagenum: 0,
  pageParams: {}, //路由页面参数
  onBack: function () {},
  menuData: [],//菜单树形结构
  menuRows: [],//菜单一维列表
  loading: false,
})

export const loginOpen = reactive({
  popup: false,
  loginnum: 0,
  user: {
    id: 0,
    name: '',
    token: '',
    role: '',
    email: '',
    phone: '',
    state: '',
  },
  setUser(user, token, refresh) {
    this.user = user;
    localStorage.setItem('flashmeUser', JSON.stringify(this.user));
    token && localStorage.setItem('flashmeToken', token);
    refresh && localStorage.setItem('flashmeRefreshToken', refresh);
  },
  getUser() {
    var user = this.user;
    if(!user||!user.id){
      user = JSON.parse(localStorage.getItem('flashmeUser'));
      this.user = user||{};
    }
    if (user) {
      return user;
    } else {
      return false;
    }
  },
  clearUser() {
    localStorage.removeItem('flashmeUser');
    localStorage.removeItem('flashmeToken');
    localStorage.removeItem('flashmeRefreshToken');
  },
  setToken(token) {
    localStorage.setItem('flashmeToken', token);
  },
  getToken() {
    var token = localStorage.getItem('flashmeToken');
    if (token) {
      return token;
    } else {
      return false;
    }
  },
  clearToken() {
    localStorage.removeItem('flashmeToken');
    localStorage.removeItem('flashmeRefreshToken');
  },
  setRefreshToken(token) {
    localStorage.setItem('flashmeRefreshToken', token);
  },
  getRefreshToken() {
    return localStorage.getItem('flashmeRefreshToken');
  },
})